package com.swordget.dao.impl;

import com.alibaba.druid.pool.DruidDataSource;

import com.swordget.commons.JdbcUtil;
import com.swordget.dao.DataStorageDao;
import com.swordget.entity.DataSource;
import com.swordget.entity.TableDesign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: DataStroageDaoImp
 * @Description: TOOD
 * @Author: 陈根
 * @Date: 2020/7/2 16:06
 * @Version
 **/
@Repository
public class DataStroageDaoImp implements DataStorageDao {
    @Autowired
    private DruidDataSource dataSource;

    @Autowired
    private JdbcUtil jdbcUtil;

    /**
     * TableManagerMysqlDao log
     */
    private final static Logger LOG = LoggerFactory.getLogger(TableManagerMysqlDao.class);

    //新增数据源方法
    @Override
    public boolean addDataSource(DataSource dataSource) {
        //新增数据
        String sql=String.format("INSERT INTO DATASOUCRE (SERIALNO,DATANAME,USERNAME,PASSWORD,IPADDRESS,DATATYPE,CREATEUSER,CREATEDATE,CREATETIME) values ('%s','%s','%s','%s','%s','%s','%s','%s','%s')",
                System.currentTimeMillis() +  String.format("%06d", Math.round(Math.random() * 1000000)),dataSource.getDataName(),dataSource.getUserName(),dataSource.getPassWord(),dataSource.getIpAddress(),dataSource.getDataType(),dataSource.getCreateUser(),dataSource.getCreateDate(),dataSource.getCreateTime());

        return jdbcUtil.execute(sql);
    }

    @Override
    public List<DataSource> findDataSoucreByPage(int rows, int page ) {
        String command = String.format("SELECT * FROM DATASOUCRE");
        try(Connection conn = dataSource.getConnection();
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery(command)){
            List<DataSource> data = new ArrayList<>();
            while (resultSet.next()) {
                data.add(DataSource.builder()
                        .serialNo(resultSet.getString("SERIALNO"))
                        .dataName(resultSet.getString("DATANAME"))
                        .userName(resultSet.getString("USERNAME"))
                        .passWord(resultSet.getString("PASSWORD"))
                        .ipAddress(resultSet.getString("IPADDRESS"))
                        .dataType(resultSet.getString("DATATYPE"))
                        .build());

            }
            System.out.println(data);
            return data;
        } catch (SQLException e) {
            LOG.error("SQL异常", e);
        }
        return null;
    }

    @Override
    public int findTotal() {
        String command = "SELECT COUNT(*) FROM DATASOUCRE";
        return jdbcUtil.executeCount(command);
    }

    //判断是否存在这个数据源
    @Override
    public boolean isExists(String dataId) {
       String sql=String.format("SELECT COUNT(*) FROM DATASOUCRE WHERE ID='%s'",dataId);
         int execute = jdbcUtil.executeCount(sql);
        return execute>0;
    }

    //删除数据源
    @Override
    public boolean dropData(String dataId) {
        String sql=String.format("DELETE FROM DATASOUCRE WHERE ID='%s'",dataId);
        boolean execute = jdbcUtil.execute(sql);
        return execute;
    }

    //修改数据源
    @Override
    public boolean editDataSource(DataSource dataSource) {
        String sql=String.format("UPDATE DATASOUCRE SET DATANAME='%s',USERNAME='%s',PASSWORD='%s',IPADDRESS='%s',DATATYPE='%s'WHERE SERIALNO='%s'",dataSource.getDataName(),dataSource.getUserName(),dataSource.getPassWord(),dataSource.getIpAddress(),dataSource.getDataType(),dataSource.getSerialNo());
        boolean execute = jdbcUtil.execute(sql);
        return execute;
    }
}
